import {MsgModel} from "../../../models/msg";
import {ChatModel} from "../../../models/chat";

const msgModel = new MsgModel()
const chatModel = new ChatModel()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    scrollLeft:0,
    tabList:['系统消息','私信'],
    sysMsgInfo:{},
    chatList:[],
    userInfo:{}
  },

  tabSelect(e) {
    let TabCur = e.currentTarget.dataset.id
    this.setData({
      TabCur: TabCur,
      scrollLeft: (e.currentTarget.dataset.id-1)*60
    })
    wx.showLoading({
      title: '正在加载中...',
    })
    this._Load(0)
  },

  _loadSysMsgList() {
    msgModel.sysMsgMainList().then(res=>{
      this.setData({
        sysMsgInfo:res.data
      })
      wx.hideLoading()
    })
  },

  _doLoadChatList(start,moreLoad) {
    chatModel.getChatList(start).then(res=>{
      if (typeof (moreLoad) == 'undefined') {
        this.setData({
          chatList:res.data
        })
      }else {
        this.setData({
          chatList:app._LoadMore(this.data.chatList,res.data)
        })
        this.unLoading(res.data);
      }
      wx.hideLoading()
    })
  },

  unLoading(data) {
    if (data.length == 0) {
      app.showInfo('没有更多了哦~')
    }
  },

  _Load(start,moreLoad) {
    wx.showLoading({
      title: '正在加载中...',
    })
    let _this = this
    const TabCur = this.data.TabCur
    switch (TabCur) {
      case 0:
        return _this._loadSysMsgList()
      case 1:
        return _this._doLoadChatList(start,moreLoad)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.checkLoginStatus()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.userInfo) {
      this._loadSysMsgList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let _this = this
    wx.showNavigationBarLoading() //在标题栏中显示加载

    //模拟加载
    setTimeout(function()
    {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      _this._Load(0)
    },1500);
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let _this = this
    const TabCur = _this.data.TabCur
    if (TabCur == 1) {
      _this._Load(this.data.chatList.length,true)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 系统消息点击
   */
  systemMsgClick:function (e) {
    app.checkLoginStatus()
    let systemMsgType=e.currentTarget.dataset.systemMsgType
    let url= ''
    if(systemMsgType == 1){
      url ="system"
    } else if (systemMsgType == 2){
      url ="appoint_receive"
    }else{
      url ="appoint_send"
    }
    wx.navigateTo({
      url: '../' + url+'/index'
    })
  },
  /**
   * 私信点击
   * @param e
   */
  privateMsgClick:function (event) {
    let id = event.currentTarget.dataset.id
    let nickname = event.currentTarget.dataset.nickname
    wx.navigateTo({
      url: '../private/index?userId='+id+'&nickname='+nickname
    })
  }
})
